package com.stack.auto_parts.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.stack.auto_parts.common.ApiResult;
import com.stack.auto_parts.entity.Orders;

import com.stack.auto_parts.entity.SupplierOrder;
import com.stack.auto_parts.service.OrderService;
import com.stack.auto_parts.service.SupplierOrderService;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Title: EchartsController
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.controller
 * @Date 2024/7/11 20:03
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("/echarts")
public class EchartsController {
    @Resource
    OrderService orderService;


    /**
     *
     * 订单每季度完成数占比（扇形图）
     * @return
     */
    @GetMapping("/getOrdersNumber")
    public ApiResult<List> getNumber() {
        List<Orders> list = orderService.selectByCondition(null,null,null,null,"0");
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for(Orders orders : list){
            Date date = orders.getUpdateTime();
            Quarter quarter = DateUtil.quarterEnum(date);
            switch(quarter){
                case Q1:
                    q1 += 1;
                    break;
                case Q2:
                    q2 += 1;
                    break;
                case Q3:
                    q3 += 1;
                    break;
                case Q4:
                    q4 += 1;
                    break;
                default:
                    break;
            }
        }
        return ApiResult .success(CollUtil.newArrayList(q1,q2,q3,q4));
    }

    /**
     *
     * 每季度采购花销（折线图）
     * @return
     */

    @GetMapping("/getTotalPrice")
    public ApiResult<List> getTotalPrice() {
        List<Orders> list = orderService.selectByCondition(null,null,null,null,"0");
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for(Orders orders : list){
            Date date = orders.getUpdateTime();
            Quarter quarter = DateUtil.quarterEnum(date);
            switch(quarter){
                case Q1:
                    q1 += orders.getTotalPrice();
                    break;
                case Q2:
                    q2 += orders.getTotalPrice();
                    break;
                case Q3:
                    q3 += orders.getTotalPrice();
                    break;
                case Q4:
                    q4 += orders.getTotalPrice();
                    break;
                default:
                    break;
            }
        }
        return ApiResult .success(CollUtil.newArrayList(q1,q2,q3,q4));
    }



    @Resource
    SupplierOrderService supplierOrderService;

    /**
     * 供货商每季度订单数量占比（扇形图）
     * @param supplier
     * @return
     */
    @GetMapping("/getOrdersNumber")
    public ApiResult<List> getSupplierNumber(String supplier) {
        List<SupplierOrder> list = supplierOrderService.selectByCondition(supplier,0,1);
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for(SupplierOrder supplierOrder : list){
            Date date = supplierOrder.getUpdateTime();
            Quarter quarter = DateUtil.quarterEnum(date);
            switch(quarter){
                case Q1:
                    q1 += 1;
                    break;
                case Q2:
                    q2 += 1;
                    break;
                case Q3:
                    q3 += 1;
                    break;
                case Q4:
                    q4 += 1;
                    break;
                default:
                    break;
            }
        }
        return ApiResult .success(CollUtil.newArrayList(q1,q2,q3,q4));
    }

    /**
     * 供货商每季度销售额（折线图）
     * @param supplier
     * @return
     */
    @GetMapping("/getTotalPrice")
    public ApiResult<List> getsupplierOrderTotalPrice(String supplier) {
        List<SupplierOrder> list = supplierOrderService.selectByCondition(supplier,0,1);
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for(SupplierOrder supplierOrder : list){
            Date date = supplierOrder.getUpdateTime();
            Quarter quarter = DateUtil.quarterEnum(date);
            switch(quarter){
                case Q1:
                    q1 += supplierOrder.getTotalPrice();
                    break;
                case Q2:
                    q2 += supplierOrder.getTotalPrice();
                    break;
                case Q3:
                    q3 += supplierOrder.getTotalPrice();
                    break;
                case Q4:
                    q4 += supplierOrder.getTotalPrice();
                    break;
                default:
                    break;
            }
        }
        return ApiResult .success(CollUtil.newArrayList(q1,q2,q3,q4));
    }



}


